package com.zk.qqrobot.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zk.qqrobot.config.Constant;
import com.zk.qqrobot.config.SSRUrlEnum;
import com.zk.qqrobot.util.AESUtil;
import com.zk.qqrobot.util.RestUtil;
import com.zk.qqrobot.util.SSRValidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.List;

@Service
public class SSRService {
    private RestTemplate restTemplate;
    final Base64.Decoder decoder = Base64.getDecoder();
    @Autowired
    private RedisService<String,String> redisService;

    public String getBaseMode(SSRUrlEnum ssrUrlEnum){
        String resp = RestUtil.forGet(ssrUrlEnum.getUrl());
        System.out.println("getBaseMode resp:" + resp);
        String decoderStr = null;
        try {
            decoderStr =  new String(decoder.decode(resp),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            System.out.println("getHeikeji1 decode error");
        }
        return SSRValidUtil.valid(decoderStr);
    }

    public String findSsrCatch(String urlCode){
        String ssr = redisService.stringGet(Constant.SSRURL_KEY + urlCode);
        return ssr;
    }

    public String ssrCatch(String urlCode, String ssr) {
        redisService.stringSet(Constant.SSRURL_KEY + urlCode,ssr,Constant.SSR_CATCH_TIMOUT);
        return Constant.SUCCESS;
    }
}
